from dj3.modules.sys.stock.models.Models import StockList
from django.views.decorators.csrf import csrf_exempt
from dj3.utils.response import  SuccessJsonResponse ,ErrorJsonResponse
from dj3.utils import jsonUtil



@csrf_exempt
def day(request):
    if request.method == 'POST':

        # dic = jsonUtil.strToJson(request.body)
        symbol =None
        symbol_type =None
        account_id =None
        # if dic.has_key['symbol']:
        #     symbol=dic['symbol']
        #
        # if dic.has_key['symbol_type']:
        #     symbol_type=dic['symbol_type']
        #
        # if dic.has_key['symbol']:
        #     account_id=dic['account_id']

        sql="select   1 id,   trading_day, sum(pnl_today) pnl_today, sum(pnl_hold) pnl_hold, sum(pnl_trade) pnl_trade  from stock_list2  where 1=1"

        if symbol!=None:
            sql+="symbol='"+symbol+"'"
        if symbol_type!=None:
            sql+="symbol_type='"+symbol_type+"'"
        if account_id!=None:
            sql+="account_id='"+account_id+"'"
        sql+=" group by   trading_day   "

        results = StockList.objects.raw(sql)
        li = []
        for result in results:
            row = {}
            row['tradingDay'] = str(result.trading_day)
            row['pnlToday'] = result.pnl_today
            row['pnlTrade'] = result.pnl_trade
            row['pnlHold'] = result.pnl_hold
            li.append(row)

        return SuccessJsonResponse(li)
    else:
        return ErrorJsonResponse(None, "访问失败")


@csrf_exempt
def time(request):
    if request.method == 'POST':
        sql= "select 1 id, grid_time, sum(pnl_today) pnl_today, sum(pnl_hold) pnl_hold, sum(pnl_trade) pnl_trade from stock_list2  where    1=1 "
        symbol = request.POST.get("symbol")
        symbol_type = request.POST.get("symbolType")
        account_id = request.POST.get("account_id")
        trading_day = request.POST.get("trading_day")

        if symbol != None:
            sql += "symbol='" + symbol + "'"
        if symbol_type != None:
            sql += "symbol_type='" + symbol_type + "'"
        if account_id != None:
            sql += "account_id='" + account_id + "'"
        if trading_day != None:
            sql += "trading_day='" + trading_day + "'"
        sql += " group by    grid_time   "

        results = StockList.objects.raw(sql)
        li = []
        for result in results:
            row = {}

            row['gridTime'] = result.grid_time
            row['pnlToday'] = result.pnl_today
            row['pnlTrade'] = result.pnl_trade
            row['pnlHold'] = result.pnl_hold
            li.append(row)
        return SuccessJsonResponse(li)
    else:
        return ErrorJsonResponse(None, "访问失败")



@csrf_exempt
def getType(request):
    if request.method == 'POST':
        results = StockList.objects.raw(
            " select 1 id, account_id from ( select distinct account_id  account_id"
            " from stock_list2 ) tab ")

        dicType={}
        li = []
        for result in results:
            row = {}
            row["value"]=str(result.account_id)
            row["label"]=str(result.account_id)
            li.append(row)
        dicType['label'] = li

        results = StockList.objects.raw(
            " select 1 id, symbol from ( select distinct symbol  symbol"
            " from stock_list2 ) tab ")
        li = []
        for result in results:
            row = {}
            row["value"]=result.symbol
            row["label"]=result.symbol
            li.append(row)
        dicType['label2'] = li


        results = StockList.objects.raw(
            " select 1 id, symbol_type from ( select distinct symbol_type  symbol_type"
            " from stock_list2 ) tab ")
        li = []
        for result in results:
            row = {}
            row["value"]=result.symbol_type
            row["label"]=result.symbol_type
            li.append(row)
        dicType['label3'] = li
        return SuccessJsonResponse(dicType)
    else:
        return ErrorJsonResponse(None, "访问失败")